package swb.api.com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import swb.api.com.repository.SysRoleMenuRepository;
import swb.api.com.repository.SysUserRoleRepository;
import swb.api.com.repository.dto.SysRole;
import swb.api.com.repository.dto.SysRoleMenu;
import swb.api.com.service.SysRoleService;
import swb.api.com.repository.SysRoleRepository;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysUserRoleRepository sysUserRoleRepository;

    @Autowired
    private SysRoleMenuRepository sysRoleMenuRepository;

    @Override
    public Page<SysRole> findAll(Pageable pageable) {
        return sysRoleRepository.findAll(pageable);
    }

    @Override
    public void save(SysRole role) {
        sysRoleRepository.save(role);
    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        List<SysRoleMenu> roleMenus = sysRoleMenuRepository.findByRoleId(roleId);
        return roleMenus.stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }

    @Override
    public void assignMenus(Long roleId, List<Long> menuIds) {
        // 先删除原有角色菜单关联
        sysRoleMenuRepository.deleteByRoleId(roleId);

        // 添加新的角色菜单关联
        for (Long menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            sysRoleMenuRepository.save(roleMenu);
        }
    }

    @Override
    public List<String> getPermissionsByUserId(Long userId) {
        // 1. 根据用户ID获取用户角色关联列表
        List<Long> roleIds = sysUserRoleRepository.findByUserId(userId)
                .stream()
                .map(swb.api.com.repository.dto.SysUserRole::getRoleId)
                .collect(Collectors.toList());

        // 2. 根据角色ID列表获取角色菜单关联列表
        List<Long> menuIds = roleIds.stream()
                .flatMap(roleId -> sysRoleMenuRepository.findByRoleId(roleId).stream())
                .map(SysRoleMenu::getMenuId)
                .distinct()
                .collect(Collectors.toList());

        // 3. 根据菜单ID列表获取菜单权限标识
        return menuIds.stream()
                .map(menuId -> {
                    // 这里需要根据实际的SysMenuRepository来获取菜单信息
                    // 假设有一个sysMenuRepository已经注入
                    // return sysMenuRepository.findById(menuId).map(SysMenu::getPermission).orElse(null);
                    // 为了简化，这里返回一个示例
                    return "permission:" + menuId;
                })
                .filter(permission -> permission != null && !permission.isEmpty())
                .collect(Collectors.toList());
    }
}
